package cn.xshi.sys.web;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;

import cn.xshi.sys.model.SysConstant;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import cn.xshi.common.annotation.Auth;
import cn.xshi.common.base.BaseAction;
import cn.xshi.common.base.BasePage;
import cn.xshi.common.base.BaseResult;
import cn.xshi.common.base.BaseSearch;
import cn.xshi.common.entity.OauthAccountEntity;
import cn.xshi.common.util.StringUtil;
import cn.xshi.sys.service.SysConstantService;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.github.pagehelper.PageInfo;
/**
 * @Desc 台平常量
 * @Author 邓纯杰
 * @CreateTime 2012-12-12 12:12:12
 */
@RestController
@RequestMapping("/sysConstant")
@Api(value = "平台常量API",tags = "平台常量API",description = "平台常量API")
public class SysConstantController extends BaseAction {

	@Resource
	private SysConstantService sysConstantService;

	/**
	* 加载初始化列表数据并分页
	* @param baseSearch
	*/
	@ApiOperation(value="查询平台常量并分页", notes="查询平台常量并分页")
	@PostMapping(value="/list")
	@Auth(value = "/sysConstant/list",authenticationType = Auth.AuthorizationType.REQUIRED_LOGIN_NOT_AUTH)
	public BasePage<List<SysConstant>> getSysConstantListByCondition(@RequestBody(required=true)BaseSearch baseSearch){
		Map<String, Object> condition = baseSearch.convert();
		commonHPager(baseSearch);
		List<SysConstant> sysConstantList = sysConstantService.getSysConstantListByCondition(condition);
		for(SysConstant sysConstant : sysConstantList){
			if(!StringUtil.isEmpty(sysConstant.getCreateId())){

				OauthAccountEntity createBy = getAccount(sysConstant.getCreateId());
				if(null != createBy){
					sysConstant.setCreateBy(createBy.getName());
				}
			}
			if(!StringUtil.isEmpty(sysConstant.getUpdateId())){
				OauthAccountEntity modifiedBy = getAccount(sysConstant.getUpdateId());
				if(null != modifiedBy){
					sysConstant.setModifiedBy(modifiedBy.getName());
				}
			}
		}
		PageInfo<SysConstant> page = new PageInfo<SysConstant>(sysConstantList);
		return outPageBootStr(page,baseSearch);
	}

	/**
	* 查询单个平台常量
	* @param id
	*/
	@ApiOperation(value="查询单个平台常量", notes="查询单个平台常量")
	@GetMapping(value="/get/{id}")
	@Auth(value = "/sysConstant/get",authenticationType = Auth.AuthorizationType.REQUIRED_LOGIN_NOT_AUTH)
	public BaseResult<SysConstant> getSysConstantById(@PathVariable("id") String id){
		SysConstant sysConstant = sysConstantService.getSysConstantById(id);
		if(!StringUtil.isEmpty(sysConstant.getCreateId())){
			OauthAccountEntity createBy = getAccount(sysConstant.getCreateId());
			if(null != createBy){
				sysConstant.setCreateBy(createBy.getName());
			}
		}
		if(!StringUtil.isEmpty(sysConstant.getUpdateId())){
			OauthAccountEntity modifiedBy = getAccount(sysConstant.getUpdateId());
			if(null != modifiedBy){
				sysConstant.setModifiedBy(modifiedBy.getName());
			}
		}
		return outDataStr(sysConstant);
	}

	/**
	 * 查询全部平台常量
	 * @return
	 */
	@ApiOperation(value="查询全部平台常量", notes="查询全部平台常量")
	@GetMapping(value="/lists")
	@Auth(value = "/sysConstant/lists",authenticationType = Auth.AuthorizationType.REQUIRED_LOGIN_NOT_AUTH)
	public BaseResult<List<SysConstant>> lists(){
		Map<String,Object> condition = new HashMap<>();
		List<SysConstant> sysConstantList = sysConstantService.getSysConstantListAllByCondition(condition);
		return outDataStr(sysConstantList);
	}
	
	/**
	* 添加
	* @param sysConstant
	*/
	@PostMapping(value="/add")
	@ApiOperation(value="创建单个平台常量", notes="创建单个平台常量")
	public BaseResult addSysConstant(@RequestBody SysConstant sysConstant){
		int i = 0;
		if(null != sysConstant){
			sysConstant.setId(toUUID());
			i= sysConstantService.addSysConstant(sysConstant);
		}
		if(i>0){
			return outAudStr(true);
		}else{
			return outAudStr(false);
		}
	}
	
	/**
	* 修改
	* @param sysConstant
	*/
	@PutMapping(value="/update")
	@ApiOperation(value="编辑单个平台常量", notes="编辑单个平台常量")
	public BaseResult updateSysConstant(@RequestBody SysConstant sysConstant){
		int i = 0;
		if(null != sysConstant){
			i= sysConstantService.updateSysConstant(sysConstant);
		}
		if(i>0){
			return outAudStr(true);
		}else{
			return outAudStr(false);
		}
	}
	
	/**
	* 删除
	* @param id
	*/
	@DeleteMapping(value="/delete")
	@ApiOperation(value="删除平台常量", notes="删除平台常量")
	public BaseResult delSysConstant(String id){
		int i = 0;
		if(!StringUtil.isEmpty(id)){
			Map<String, Object> condition = new HashMap<String, Object>();
			condition.put("id",id.split(","));
			i= sysConstantService.delSysConstant(condition);
		}
		if(i>0){
			return outAudStr(true);
		}else{
			return outAudStr(false);
		}
	}

	/**
	 * 根据类型查找常量集合
	 * @return
	 */
	@ApiOperation(value="根据类型平台常量", notes="根据类型平台常量")
	@GetMapping(value="/list/{type}")
	@Auth(value = "/sysConstant/list/type",authenticationType = Auth.AuthorizationType.REQUIRED_LOGIN_NOT_AUTH)
	public BaseResult<List<SysConstant>> getSysConstantList(@PathVariable("type")String type){
		Map<String, Object> condition = new HashMap<String, Object>();
		condition.put("type", type);
		List<SysConstant> sysConstantList = sysConstantService.getSysConstantListByCondition(condition);
		return outItemsStr(sysConstantList);
	}
}
